<?php
/**
 * @copyright (c) 2013 aircheng.com
 * @file json_class.php
 * @brief 文件处理
 * @author chendeshan
 * @date 2013-4-15
 * @version 1.0
 */

/**
 * @class JSON
 * @brief JSON json数据格式的处理
 */
class JSON
{
	//第三方JSON类库实例
	private static $_jsonInstance = null;

	/**
	 * @brief json数据格式编码,支持中文原文转换
	 * @param $param max 要编码转换的数据
	 * @return String json数据格式
	 */
	public static function encode($param)
	{
		if(version_compare(phpversion(),'5.4.0') >= 0)
		{
			return json_encode($param,JSON_UNESCAPED_UNICODE);
		}

		$result = '';
		if(function_exists('json_encode'))
		{
			$result = json_encode($param);
		}
		else
		{
			$jsonObject = self::getJsonInstance();
			$result = $jsonObject->encodeUnsafe($param);
		}
		//对于中文的转换
		return preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $result);
	}

	/**
	 * @brief 解析json数据格式
	 * @param $string String 要解析的json串
	 * @return max php数据格式
	 */
	public static function decode($string)
	{
		if(strpos($string,"\t") !== false)
		{
			$string = str_replace("\t",'',$string);
		}

		if(function_exists('json_decode'))
		{
			return json_decode($string,true);
		}

		$jsonObject = self::getJsonInstance();
		return $jsonObject->decode($string);
	}

	/**
	 * @brief 初始化第三方JSON类库
	 * @return Object 第三方json处理类
	 */
	private static function getJsonInstance()
	{
		if(self::$_jsonInstance == null)
		{
			include(dirname(__FILE__).'/Services_JSON-1.0.3/JSON.php');
			self::$_jsonInstance = new Services_JSON();
		}
		return self::$_jsonInstance;
	}

	/**
     * 返回一个json的提示
     * @param $code
     * @param $message
     * @param $data
     * @return string
     */
    public static function jsonResponse($code, string $message, $data): array
    {
        $content = [ 'code' => $code, 'msg' => $message, 'data' => $data];

        return $content;
    }

    /**
     * api接口成功时的返回
     * @param array $data
     * @return string
     */
    public static function jsonSuccess($data = [])
    {
        echo json_encode(self::jsonResponse(200, '请求成功', $data),JSON_UNESCAPED_UNICODE);
        exit;
    }

    /**
     * 当api接口出现问题时抛出的异常状态
     * @param $code
     * @param $message
     * @param array $data
     * @return string
     */
    public static function jsonErr(int $code, $message = '', $data = []): string
    {
        if (!empty($message)) {
			echo json_encode(self::jsonResponse($code, $message, $data),JSON_UNESCAPED_UNICODE);
			exit;
		}
		echo json_encode(self::jsonResponse($code, self::errCode($code), $data),JSON_UNESCAPED_UNICODE);
		exit;
    }

    public static function errCode($code = 0)
    {
        switch ($code) {

            // 系统级错误、基础业务逻辑错误
            case 405: $error = "请求资源不被允许"; break;
            case 500: $error = "系统错误，存在系统验证错误"; break;
            case 3817: $error = "请求已受理"; break;
            /********** 用户系统 **********/
            case 100001: $error = "必填项校验未通过，请核对递交信息"; break;
            case 100002: $error = "数据请求失败，请核对递交信息"; break;
            case 100003: $error = "用户token为空"; break;
            case 100004: $error = "用户token错误或过期失效"; break;
            case 100005: $error = "填写正确的商户真实全称"; break;
            case 100006: $error = "交易金额不能为空"; break;
            case 100007: $error = "交易金额必须为数字"; break;
            case 100008: $error = "交易用户信息为空"; break;
            case 100009: $error = "交易用户信息过长"; break;
            case 100010: $error = "交易用户信息过短"; break;
            case 100011: $error = "通知地址为空"; break;
            case 100012: $error = "跳转地址为空"; break;
            case 100013: $error = "MAC验证失败"; break;
            case 100014: $error = "缓存数据库超出使用限制"; break;
            case 100015: $error = "业务端数据缓存失败"; break;
            case 100016: $error = "数据库操作失败"; break;
            case 100017: $error = "数字签名验证失败"; break;
            case 100018: $error = "项目缓存未命中"; break;
            case 100019: $error = "查询交易类型为空"; break;
            case 100020: $error = "查询交易类型不正确"; break;
            case 100021: $error = "渠道信息获取失败"; break;
            case 100022: $error = "请求交易类型错误"; break; // UI API
            case 100023: $error = "目录权限不够"; break;
            case 100024: $error = "23:45 ~ 00:15暂停交易"; break;
            case 100025: $error = "获取短息验证码失败"; break;
            case 100026: $error = "当前用户已签约绑定，请勿重复绑定"; break;
            case 100027: $error = "签约失败"; break;
            case 100028: $error = "当前用户未绑定，请核对用户绑定信息"; break;
            case 100029: $error = "解约失败"; break;
            case 100030: $error = "当前用户签约有误，请联系管理处理！"; break;
            case 100031: $error = "当前用户已签约"; break;
            case 100032: $error = "当前支付已经是默认支付"; break;
            case 100033: $error = "当前银行卡存在其他绑定用户，请联系管理员后台解绑"; break;
            case 100034: $error = "当前对账已存在"; break;
            case 100035: $error = "当前订单号，请勿在15秒能继续使用！！！"; break;
            case 500001: $error = "订单不存在"; break;
            case 500002: $error = "更新默认支付异常"; break;
            case 500003: $error = "签约编码为空"; break;
            case 500004: $error = "业务订单号已完成，请勿重复交易"; break;
            case 500005: $error = "交易失败"; break;
            case 500006: $error = "交易未完成"; break;
            case 900024: $error = "数据模型不存在"; break;
            default: $error = '未知错误';
        }
        return $error;
    }
}